package org.farm2.service.utils;

import org.apache.commons.lang3.StringUtils;
import org.farm2.skc.domain.SkcTag;
import org.farm2.skc.service.SkcTagServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 检索词解析器
 */
@Component
public class FarmSearchAnalysisUtils {
    @Autowired
    private SkcTagServiceInter skcTagServiceImpl;

    /**
     * 从检索词中解析出标签key，可能是key或标签名称
     *
     * @param word
     * @return
     */
    public String getTagKey(String word) {
        //  TAG-前端开发-XXX-
        if (StringUtils.isBlank(word)) {
            return "";
        }
        if (word.indexOf("TAG-") != 0) {
            return "";
        }
        String[] words = word.split("-");
        String tag = null;
        if (words.length >= 2) {
            tag = words[1];
        }
        if (StringUtils.isNotBlank(tag)) {
            //查询是否有系统tag
            List<SkcTag> tags = skcTagServiceImpl.getTagsByName(tag);
            if (tags.size() > 0) {
                tag = tags.get(0).getTagkey();
            }
        }
        return tag;
    }
}
